package com.augurit.gzcgwsp.jzfqwczz.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.jzfqwczz.common.convert.YwYnBusinfoNssyConvertor;
import com.augurit.gzcgwsp.jzfqwczz.common.dao.YwYnBusinfoNssyDao;
import com.augurit.gzcgwsp.jzfqwczz.common.entity.YwYnBusinfoNssy;
import com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnBusinfoNssyService;
import com.augurit.gzcgwsp.jzfqwczz.common.web.form.YwYnBusinfoNssyForm;
import com.augurit.gzcgwsp.ljzyz.service.IYwYnBusinfoService;
import com.augurit.gzcgwsp.ljzyz.web.form.YwYnBusinfoForm;

@Service
@Transactional
public class YwYnBusinfoNssyServiceImpl implements IYwYnBusinfoNssyService {
	
	@Autowired
	private YwYnBusinfoNssyDao YwYnBusinfoNssyDao;
	@Autowired
	private IYwYnBusinfoService ywYnBusinfoService;
	@Transactional(readOnly = true)
	public YwYnBusinfoNssyForm get(Long id) {
		YwYnBusinfoNssy entity = YwYnBusinfoNssyDao.get(id);
		return YwYnBusinfoNssyConvertor.convertVoToForm(entity);
	}
	
	public void delete(Long... ids) {
		YwYnBusinfoNssyDao.delete(ids);
	}


	public void save(YwYnBusinfoNssyForm  form,boolean updateBus) {
		this.save(form);
		//改车辆的状态
		if(updateBus){
			if("合格".equals(form.getZhsyjl())){
				YwYnBusinfoForm busform=ywYnBusinfoService.get(form.getBusId());
				busform.setNssts("2");//审核完成
				ywYnBusinfoService.save(busform);
			}
		}
	}
	
	public void save(YwYnBusinfoNssyForm... forms) {
		if(forms != null)
			for(YwYnBusinfoNssyForm form : forms){
				this.save(form);
			}
	}
	
	public void update(YwYnBusinfoNssyForm form) {
	    YwYnBusinfoNssyForm dbform = null;
	    if (form.getId() != null && form.getId() != 0) dbform = this.get(form.getId());
	    
	    if (dbform == null) {
	        this.save(form);
	    } else {
	        new DozerBeanMapper().map(form, dbform);
            this.save(dbform);
	    }
	}
	
	public void save(YwYnBusinfoNssyForm form){
		if(form != null){
			YwYnBusinfoNssy entity = null;
			
			if(form != null && form.getId() != null){
				entity = YwYnBusinfoNssyDao.getYwYnBusinfoNssy(form.getId());
				if(entity.getId()==null){
					entity = new YwYnBusinfoNssy();
					entity.setId(form.getId());
				}
			}else{
				entity = new YwYnBusinfoNssy();
			}
			
			YwYnBusinfoNssyConvertor.convertFormToVo(form, entity);
			YwYnBusinfoNssyDao.save(entity);
			form.setId(entity.getId());
		}
	
	}

	@Transactional(readOnly = true)
	public Page<YwYnBusinfoNssyForm> search(Page<YwYnBusinfoNssyForm> page, YwYnBusinfoNssyForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("from YwYnBusinfoNssy ps where 1=1");
		List values = new ArrayList();
		
		if(form != null){
			if(form.getId() != null){
		    	hql.append(" and ps.getId="+form.getId());
		    }
		    if(form.getBusId() != null){
		    	hql.append(" and ps.busId="+form.getBusId());
		    }
		}
		
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		YwYnBusinfoNssyDao.findPage(pg, hql.toString(), values);
		
		List<YwYnBusinfoNssyForm> list = YwYnBusinfoNssyConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	@Transactional(readOnly = true)
	public List<YwYnBusinfoNssyForm> search(YwYnBusinfoNssyForm form) {		
		StringBuffer hql = new StringBuffer("from YwYnBusinfoNssy ps where 1=1");
		List values = new ArrayList();		
		if(form != null){
			if(form.getId() != null){
		    	hql.append(" and ps.getId="+form.getId());
		    }
		    if(form.getBusId() != null){
		    	hql.append(" and ps.busId="+form.getBusId());
		    }
		}
		List<YwYnBusinfoNssy> ywYnBusinfoNssyList = YwYnBusinfoNssyDao.find( hql.toString(), values);
		List<YwYnBusinfoNssyForm> list = YwYnBusinfoNssyConvertor.convertVoListToFormList(ywYnBusinfoNssyList);
		return list;
	}
	
	@Transactional(readOnly = true)
	public Page<YwYnBusinfoNssyForm> search(Page<YwYnBusinfoNssyForm> page, List<PropertyFilter> filters) {
		Page<YwYnBusinfoNssy> pg = PageUtils.newPageInstance(page);
		
		YwYnBusinfoNssyDao.findPage(pg, filters);
		
		List<YwYnBusinfoNssyForm> list = YwYnBusinfoNssyConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	public void updateYwYnBusinfoNssy(YwYnBusinfoNssyForm form) {
	    YwYnBusinfoNssyForm dbform = null;
	    if (form.getId() != null && form.getId() != 0)
	    	dbform = this.get(form.getId());
	    if (dbform != null) {
			YwYnBusinfoNssy entity = null;
			entity = YwYnBusinfoNssyDao.getYwYnBusinfoNssy(dbform.getId());
			YwYnBusinfoNssyConvertor.convertFormToVo(form, entity);
			if(entity!=null){
				YwYnBusinfoNssyDao.updateYwYnBusinfoNssy(entity);
			}
	    }
	}

    public void saveEntity(YwYnBusinfoNssy entity) {
       this.YwYnBusinfoNssyDao.save(entity);
        
    }

	/**
	 * 保存现场勘察表返回id
	 */
	public Long saveEnterpriseInfo(YwYnBusinfoNssyForm form) {
		YwYnBusinfoNssy entity =new YwYnBusinfoNssy();
		
		YwYnBusinfoNssyConvertor.convertFormToVo(form, entity);
		
		return YwYnBusinfoNssyDao.saveEntity(entity);
	}

	@Override
	public Page<YwYnBusinfoNssyForm> getNsDj(Page<YwYnBusinfoNssyForm> page,
			YwYnBusinfoNssyForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("select ps,info from YwYnBusinfoNssy ps,YwYnBusinfo info where info.id=ps.busId");
		List values = new ArrayList();
		
		if(form != null){
			if(form.getId() != null){
		    	hql.append(" and ps.getId="+form.getId());
		    }
		    if(form.getBusId() != null){
		    	hql.append(" and ps.busId="+form.getBusId());
		    }
		}
		hql.append(" and ps.sts='1'");
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		YwYnBusinfoNssyDao.findPage(pg, hql.toString(), values);
		List<YwYnBusinfoNssyForm> list = YwYnBusinfoNssyConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	@Override
	public YwYnBusinfoNssyForm getEntity(String busId,String sts) {
		String sql=" from YwYnBusinfoNssy where busId="+busId+" and sts='"+sts+"'";
		List<YwYnBusinfoNssy> list=YwYnBusinfoNssyDao.find(sql);
		YwYnBusinfoNssyForm form=new YwYnBusinfoNssyForm();
		if(list.size()>0){
			YwYnBusinfoNssy entity=list.get(0);
			form=YwYnBusinfoNssyConvertor.convertVoToForm(entity);
		}
		return form;
	}

	@Override
	public void doAllnssy(String ids) {
		String[] idsArr=ids.split(",");
		for(String busId:idsArr){
			YwYnBusinfoNssyForm form=getEntity(busId, "1");
			form.setBusId(Long.valueOf(busId));
			form.setXszsfgx("有");
			form.setBzp("合格");
			form.setSfns("有");
			form.setCp("合格");
			form.setDg("合格");
			form.setLunt("合格");
			form.setCrck("合格");
			form.setBzp("合格");
			form.setCr("合格");
			form.setYs("合格");
			form.setCmpz("合格");
			form.setSgmfx("合格");
			form.setWmxwm("合格");
			form.setFlcwg("合格");
			form.setSyxj("合格");
			form.setZhsyjl("合格");
			form.setJjzdSyxj("合格");
			form.setShwjSyxj("合格");
			form.setSts("2");
			this.save(form,true);
		}
		
	}

	@Override
	public List<YwYnBusinfoNssy> getNssyByBusId(Long busId, String sts) {
		String sql=" from YwYnBusinfoNssy where busId="+busId;
		if(StringUtils.isNotBlank(sts)){
			sql+=" and sts='"+sts+"'";
		}
		sql+=" order by id desc";
		return this.YwYnBusinfoNssyDao.find(sql);
	}




}
